package com.wy.cms.util;

import com.wy.cms.template.bean.IndexBean;
import freemarker.template.*;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by clei on 16/4/20.
 */
public class FreemarkerUtil {

//    private final Log logger = LogFactory.getLog(getClass());

    public static Configuration getConfiguration(String templatePath) throws Exception {
        Configuration configuration = new Configuration();

        configuration.setDirectoryForTemplateLoading(new File(templatePath));
        configuration.setObjectWrapper(new DefaultObjectWrapper());
        configuration.setDefaultEncoding("UTF-8");

        return configuration;
    }

//    public static Template getTemplate(Configuration configuration,String templateName) throws IOException, TemplateException {
//        Template template = configuration.getTemplate(templateName);
//
//
//        return template;
//    }
//
//    public static void writerFile(String templatePath,String templateName,String writerFilePath) throws IOException, TemplateException {
//        Configuration configuration = getConfiguration(templatePath);
//        Template template = getTemplate(configuration,templateName);
//        Map<String, Object> paramMap = new HashMap<String, Object>();
//        paramMap.put("description", "我正在学习使用Freemarker生成静态文件！");
//        Writer writer  = new OutputStreamWriter(new FileOutputStream(writerFilePath),"UTF-8");
//        template.process(paramMap, writer);
//    }

    public static void processTemplate(Template template, Map model, File outputFile) throws Exception {
        Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outputFile),"UTF-8"));
        template.process(model,out);
        out.close();
    }

    public static String processTemplateString(String templateString,Map model,Configuration conf) {
        StringWriter out = new StringWriter();
        try {
            Template template = new Template("templateString...",new StringReader(templateString),conf);
            template.process(model, out);
            return out.toString();
        }catch(Exception e) {
            throw new IllegalStateException("cannot process templateString:"+templateString+" cause:"+e,e);
        }
    }

    public static void main(String[] args){
        try {
            Configuration configuration = getConfiguration("template");
            Template template = configuration.getTemplate("index.jsp");
            IndexBean indexbean = new IndexBean();
            indexbean.setTitle("标题");
            indexbean.setKeywords("关键字");
            indexbean.setDescription("描述");
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("table", indexbean);
            processTemplate(template,paramMap,new File("/Users/clei/Documents/freemarkerTemp/temp.jsp"));
//            System.out.println(processTemplateString("123123123${table.title}123123123", paramMap, configuration));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
